#include <wfrest/HttpServer.h>
#include <iostream>
#include <chrono>

#include "ppconsul/agent.h"

using ppconsul::Consul;
using namespace ppconsul::agent;

void timer_callback(WFTimerTask* task)
{
    Agent* agent = (Agent*)task->user_data;
    agent->servicePass("UserService2");

    WFTimerTask* next = WFTaskFactory::create_timer_task(7, 0, timer_callback);
    next->user_data = task->user_data;

    series_of(task)->push_back(next);
}

int main()
{
    using namespace wfrest;

    HttpServer server {};

    if (server.start(8888) == 0) {

        // 向注册中心注册服务
        Consul consul { "http://127.0.0.1:8500", ppconsul::kw::dc="dc1" };
        Agent agent { consul };
        // 注册服务 
        agent.registerService(
            kw::id = "UserService2",
            kw::name = "UserService",
            kw::address = "127.0.0.1",
            kw::port = 8888,
            kw::check = TtlCheck(std::chrono::seconds{ 10 })
        );
        
        // 定时发送心跳包
        WFTimerTask* timerTask = WFTaskFactory::create_timer_task(7, 0, timer_callback);
        timerTask->user_data = &agent;

        timerTask->start();

        getchar();
        server.stop();
    } else {
        std::cerr << "Error: cannot start server!\n";
        std::exit(1);
    }
    return 0;
}

